package MyLinkedList;

public class MyList implements SingleLinkedList{
    static class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    public void createList(){
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(22);
        ListNode node3 = new ListNode(33);
        ListNode node4 = new ListNode(44);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null){
            head = node;
            return;
        }
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(cur == null){
            head = node;
            return;
        }
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }

    @Override
    public boolean addIndex(int index, int data) {
        if(index < 0 || index > this.size() + 1){
            return false;
        }
        if(index == 0){
            addFirst(data);
            return true;
        }
        if(index == size()){
            addLast(data);
            return true;
        }
        ListNode node = new ListNode(data);
        ListNode cur = head;
        int num = 0;
        while(cur.next != null && num != index - 1){
            cur = cur.next;
            if(cur.next == null && num < index - 1){
                return false;
            }
            num++;
        }
        node.next = cur.next;
        cur.next = node;
        return true;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        if(cur == null){
            return false;
        }
        while(cur.next != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        if(cur.val == key){
            return true;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        if (cur.val == key) {
            if (cur == head) {
                head = cur.next;
            }
        }
        while (cur.next != null) {
            if (cur.next.val == key) {
                if(cur.next.next == null){
                    cur.next = null;
                    break;
                }else {
                    cur.next = cur.next.next;
                    break;
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        if(head == null){
            return;
        }
        while(this.contains(key)){
            this.remove(key);
        }
    }

    @Override
    public int size() {
        int num = 1;
        ListNode cur = head;
        while(cur.next != null){
            num++;
            cur = cur.next;
        }
        return num;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        head = null;
    }
}
